package com.gaoyi.crm.control;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.res.ResourcesCompat;

import com.gaoyi.crm.R;

public class DrawableCenterTextView extends AppCompatTextView {

    private Integer mSort;

    /**
     * 字体高度
     */
    private float mFontHeight;
    /**
     * 位图集合
     */
    private Drawable[] mDrawables;
    private Drawable mDrawable, mDescDrawable, mAscDrawable,mDefaultDrawable;
    /**
     * 字体偏移数据
     */
    private float mOffSize;
    private int mId;

    private OnChangeListener mOnChangeListener;

    public void setOnChangeListener(OnChangeListener onChangeListener) {
        this.mOnChangeListener = onChangeListener;
    }

    public DrawableCenterTextView(Context context) {
        super(context);
        init(context);
    }

    public DrawableCenterTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DrawableCenterTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        mDrawables = getCompoundDrawablesRelative();
        if (mDrawables[0] != null || mDrawables[2] != null) {
            // 左、右
            setGravity(Gravity.CENTER_VERTICAL | (mDrawables[0] != null ? Gravity.START : Gravity.END));
        } else if (mDrawables[1] != null || mDrawables[3] != null) {
            // 上、下
            setGravity(Gravity.CENTER_HORIZONTAL | (mDrawables[1] != null ? Gravity.TOP : Gravity.BOTTOM));
            Paint.FontMetrics fm = getPaint().getFontMetrics();
            mFontHeight = (float) Math.ceil(fm.descent - fm.ascent);
        }
        super.onLayout(changed, left, top, right, bottom);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        int drawablePadding = getCompoundDrawablePadding();
        if (mDrawables[0] != null) {
            mDrawable = mDrawables[0];
            // 左
            float textWidth = getPaint().measureText(getText().toString());
            int drawableWidth = mDrawable.getIntrinsicWidth();
            float bodyWidth = textWidth + drawableWidth + drawablePadding;
            if (TextUtils.isEmpty(getText())) {
                bodyWidth = drawableWidth;
            }
            mOffSize = (getWidth() - bodyWidth) / 2;
            canvas.translate(mOffSize, 0);

        } else if (mDrawables[2] != null) {
            mDrawable = mDrawables[2];
            // 右
            float textWidth = getPaint().measureText(getText().toString());
            int drawableWidth = mDrawable.getIntrinsicWidth();
            float bodyWidth = textWidth + drawableWidth + drawablePadding;
            if (TextUtils.isEmpty(getText())) {
                bodyWidth = drawableWidth;
            }
            mOffSize = (bodyWidth - getWidth()) / 2;
            canvas.translate(mOffSize, 0);
        } else if (mDrawables[1] != null) {
            mDrawable = mDrawables[1];
            // 上
            int drawableHeight = mDrawable.getIntrinsicHeight();
            float bodyHeight = mFontHeight + drawableHeight + drawablePadding;
            if (TextUtils.isEmpty(getText())) {
                bodyHeight = drawableHeight;
            }
            mOffSize = (getHeight() - bodyHeight) / 2;
            canvas.translate(0, mOffSize);
        } else if (mDrawables[3] != null) {
            mDrawable = mDrawables[3];
            // 下
            int drawableHeight = mDrawable.getIntrinsicHeight();
            float bodyHeight = mFontHeight + drawableHeight + drawablePadding;
            if (TextUtils.isEmpty(getText())) {
                bodyHeight = drawableHeight;
            }
            mOffSize = (bodyHeight - getHeight()) / 2;
            canvas.translate(0, mOffSize);
        }
        super.onDraw(canvas);
    }

    public float getOffSize() {
        return mOffSize;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (mDrawable != null) {
                if (mSort == null) {
                    setCompoundDrawablesRelative(null, null, mDescDrawable, null);
                    mSort = 0;
                }else if (mSort == 0){
                    setCompoundDrawablesRelative(null, null, mAscDrawable, null);
                    mSort = 1;
                }else if (mSort == 1){
                    setCompoundDrawablesRelative(null, null, mDefaultDrawable, null);
                    mSort = null;
                }
                if (mOnChangeListener != null) {
                    mOnChangeListener.onChangeListener(mId, mSort);
                }
            }
        }
        return super.onTouchEvent(event);
    }

    /**
     * 初始化
     */
    void init(Context context) {
        mId = getId();
        setClickable(true);
        mDescDrawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.ic_desc, null);
        if (mDescDrawable != null) {
            mDescDrawable.setBounds(0, 0, mDescDrawable.getIntrinsicWidth(), mDescDrawable.getIntrinsicHeight());
        }
        mAscDrawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.ic_asc, null);
        if (mAscDrawable != null) {
            mAscDrawable.setBounds(0, 0, mAscDrawable.getIntrinsicWidth(), mAscDrawable.getIntrinsicHeight());
        }
        mDefaultDrawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.ic_sort, null);
        if (mDefaultDrawable != null) {
            mDefaultDrawable.setBounds(0, 0, mDefaultDrawable.getIntrinsicWidth(), mDefaultDrawable.getIntrinsicHeight());
        }
    }

    public interface OnChangeListener{
        void onChangeListener(int id,Integer sort);
    }
}
